package com.example.deliveryapp;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import de.hdodenhof.circleimageview.CircleImageView;

public class SearchScreenFragment extends Fragment {
    private String userPhone;
    private TextView PhoneNumber;
    private CircleImageView head_on_search;

    public SearchScreenFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        final View view = inflater.inflate(R.layout.searchscreen, container, false);

        // 初始化手机号文本视图
        PhoneNumber = view.findViewById(R.id.phone_on_search); // 确保这是正确的 ID

        // 从 SharedPreferences 获取手机号
        SharedPreferences sharedPreferences = getActivity().getSharedPreferences("UserPrefs", Context.MODE_PRIVATE);
        // 将默认值改为 "No login" 而不是 null
        String userPhone = sharedPreferences.getString("userPhone", "No login");
        boolean isLoggedIn = sharedPreferences.getBoolean("isLoggedIn", false);

        // 如果用户未登录，确保userPhone设置为"No login"
        if (!isLoggedIn) {
            userPhone = "No login";
        }

        // 根据登录状态设置手机号文本
        if (isLoggedIn) {
            PhoneNumber.setText(userPhone); // 用户已登录，显示手机号
        } else {
            PhoneNumber.setText("No login"); // 用户未登录，显示"No login"
        }


        // 手机号点击事件
        PhoneNumber.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isLoggedIn) {
                    // 如果已登录，则跳转到个人主页
                    ((MainPageActivity) getActivity()).showMyAccountPageFragment();
                } else {
                    // 如果未登录，则跳转到登录界面
                    Intent intent = new Intent(getActivity(), SignInActivity.class);
                    startActivity(intent);
                }
            }
        });
        //初始化头像视图
        head_on_search = view.findViewById(R.id.head_on_search);
        //头像点击事件
        head_on_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isLoggedIn) {
                    // 如果已登录，则跳转到个人主页
                    ((MainPageActivity) getActivity()).showMyAccountPageFragment();
                } else {
                    // 如果未登录，则跳转到登录界面
                    Intent intent = new Intent(getActivity(), SignInActivity.class);
                    startActivity(intent);
                }
            }
        });



        return view;
    }
}